<template>
  <!-- 导航 -->
    <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
        <el-col :span="24">
            <el-breadcrumb :separator-icon="ArrowRight">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>个人中心</el-breadcrumb-item>
            </el-breadcrumb>
        </el-col>
    </el-row>
  <el-row>
      <el-descriptions title="Width horizontal list" border>
          <el-descriptions-item
                  :rowspan="2"
                  :width="140"
                  label="Photo"
                  align="center"
          >
              <el-upload
                      class="avatar-uploader"
                      action="http://localhost:8080/upload"
                      :show-file-list="false"
                      name="uploadFile"
                      :headers="headers"
                      :on-success="handleAvatarSuccess"
              >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                  <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
          </el-descriptions-item>
          <el-descriptions-item label="用户名">{{user.username}}</el-descriptions-item>
          <el-descriptions-item label="真实姓名">{{user.realName}}</el-descriptions-item>
          <el-descriptions-item label="注册时间">{{user.createTime}}</el-descriptions-item>
          <el-descriptions-item label="状态">
              <template v-for="status in userStatus">
                    <el-tag v-if="user.userStatus == status.dictDataValue" size="small">{{status.dictDataTag}}</el-tag>
              </template>
          </el-descriptions-item>
      </el-descriptions>
  </el-row>
</template>

<script setup>

// 申明一个对象来封装当前用户信息
import {onMounted, ref} from "vue";
import dictRequest   from "@/api/dict.js";
import sysRequest from "@/api/system.js";
import {ElMessage} from "element-plus";

let user = ref({});
let userStatus = ref([]);
const dictType = 'data_status';
// 上传成功之后的图片地址
const imageUrl = ref('')
// 上传图片的请求头信息
let headers=ref({token:localStorage.getItem("token")})
// 加载数据的函数
// 加载数据状态字典的函数
async function loadDataStatus(){
    // 同步加载数据状态字典信息
  let dict = await dictRequest.getDictByTypeAsync(dictType);
    console.log(dict)
  userStatus.value = dict.data.dictDataList;
};

/**
 * 加载用户信息
 */
function loadUser(){
    sysRequest.getUser(res=>{
        user.value = res.data;
        imageUrl.value = user.value.userHead;
    })
}

/**
 * 上传成功之后的处理函数
 */
function handleAvatarSuccess(res){
    // 这里的res就是后端响应的结果，其中属性data就是上传成功之后的图片的访问地址
    imageUrl.value = res.data;
    // 上传完成之后，就要修改用户的头像地址
    user.value.userHead = res.data;
    //  更新用户信息
    sysRequest.updateUser(user.value,res=>{
        if(res.code == '200'){
            ElMessage({
                type:"success",
                message:"更新头像成功"
            });
        }
    })
}

onMounted(()=>{
    loadDataStatus();
    loadUser();
})

</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
 .avatar-uploader .el-upload {
     border: 1px dashed var(--el-border-color);
     border-radius: 6px;
     cursor: pointer;
     position: relative;
     overflow: hidden;
     transition: var(--el-transition-duration-fast);
 }

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>